/**
 * 提交申请弹窗控制器
 */
import tem from './index.html'
import './index.less'
import $_fn from '~plugins'
class hintoperCtrl {
  constructor($scope,$rootScope,http,$ui,$state,$location,$timeout) {
    $scope.param = {}
    $scope.redErr = {}
    console.log('$scope.params', $scope.params,$location,$location.path(),$location.search())
    $scope.cancel = function () {
      $rootScope.$uiDirectiveModal.close() //关闭方法
    }
    //警告提示
    $scope.errorToast = function(text) {
      $ui.toast({text:text,type:'warning',position:'center',appendTo:'.wp-container'});
    }
    //成功提示
    $scope.successToast = function(text) {
        $ui.toast({text:text,type:'success',position:'center',appendTo:'.wp-container'});
    }
    $scope.remHight = function(){                                                 //去除报错高亮
      for(var i in $scope.redErr){
        $scope.redErr[i] = false
      }
    }
    $scope.init = function(){
      $scope.sectIds = $_fn._fromCheckValue('ch_id')
      console.log('ids:', $scope.sectIds)
      if($scope.params.batchOper && !$scope.sectIds){

        $scope.errorToast('请先选择操作项！')
        $scope.cancel()
      }

      if($scope.params.nowOper=='确认到账'){
        $scope.nowState = 1
        $scope.url = 'confirmationAccount.do'
      }
      else if ($scope.params.nowOper=='取消收款'){
        $scope.nowState = 2
        $scope.url = 'cancelPayment.do'
      }
      else if ($scope.params.nowOper=='批量删除'){
        $scope.nowState = 3
        $scope.url = 'deleteUser.do'
      }
      else if ($scope.params.nowOper=='批量恢复'){
        $scope.nowState = 4
        $scope.url = 'updateUserEnable.do'
      }
      else if ($scope.params.nowOper=='批量冻结'){
        $scope.nowState = 5
        $scope.url = 'updateUserEnable.do'
      }
    }

    $scope.init()

    $scope.ok=function (state) {
      if(!$scope.params.reason){
        $scope.redErr.reason=true
        $scope.errorToast('请填写退回原因')
        return
      }
      let formData = {}
      //批量操作
      if($scope.nowState==1 || $scope.nowState==2){
        formData.pay_id = $scope.sectIds
      }

      if($scope.nowState==3){
        //删除冻结的账号 locked=2
        formData.ids = $_fn._fromCheckValue('ch_id',null,2)
      }

      if($scope.nowState==4){
        formData.ids = $scope.sectIds
        formData.locked = 1
      }

      if($scope.nowState==5){
        formData.ids = $scope.sectIds
        formData.locked = 2
      }

      $scope.params.reason && (formData.reason=$scope.params.reason)
      // if($scope.params.batchOper)
      // $scope.param.oid = $scope.params.oid
      // $scope.param.remark = $scope.params.remark
      http.post($scope.url, formData, function (data) {
        if(data.errcode == 1){
          $scope.successToast(data.errmsg)
          $scope.cancel()
          $rootScope.genXin1="2";
          //回调当前页面请求数据函数
          if($rootScope.refreshPage){
            $rootScope.refreshPage()
          }

        }else{
          $scope.errorToast(data.errmsg || '提交失败！')
        }
      })
    }
  }
}


hintoperCtrl.$inject = ['$scope','$rootScope','http','$ui','$state','$location','$timeout','$interval']
module.exports = angular.module('directive.hintoper', []).directive('hintoper', function() {
  return {
    restrict: 'E',
    template: tem,
    replace:true,
    controller: hintoperCtrl,
    scope: {
      params: '<'
    },
    link:function(){
      $_fn.clickBind()
    }
  }
}).name